/*   The MIT License
 *   
 *   Carina Engine
 *   Copyright (c) 2009 2010 2011 2012 Zdravko Velinov
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#ifndef GRAPHICS_ENGINE_HH_
#define GRAPHICS_ENGINE_HH_

#include "carina/common/logging.hh"
#include "carina/settings-file.hh"
#include "carina/lighting.hh"
#include "carina/resources.hh"
#include "carina/world-manager.hh"
#include "carina/node-manager.hh"
#include "carina/window.hh"

namespace Carina
{
class GraphicsEngineContext;
typedef shared_ptr<GraphicsEngineContext> GraphicsEngineContextPtr;

enum GraphicsEngineFlags
{
    CE_GRAPHICS_LOAD_CONTEXTS = 1,
    CE_GRAPHICS_DEFAULT = CE_GRAPHICS_LOAD_CONTEXTS
};

typedef DataVector<GraphicsEngineContextPtr> ContextVector;

UVT_ENUM_BIND(GraphicsEngineContextPtr, CE_UVT_STRUCT, 1)
UVT_ENUM_BIND(ResourceCachePtr, CE_UVT_STRUCT, 1)

//! Convenience class for initializing the graphics engine.
class GraphicsEngine
{
    WindowManagerPtr    m_WindowManager;    //!< Window manager.
    RenderingSubsystem  m_RenderingSystem;  //!< Rendering subsystem.

    //! The current rendering library which is determined by the configuration file.
    RenderingLibraryPtr m_RenderingLibrary;

    //! Settings associated with this graphics engine.
    SettingsFile        m_Settings;
    
public:
    //! Constructor.
    GraphicsEngine(const string& _icon = string(), unsigned flags = CE_GRAPHICS_LOAD_CONTEXTS);
    
    //! Returns pointer to the window manager.
    WindowManagerPtr getWindowManager() { return m_WindowManager; }

    //! Returns the current rendering library.
    RenderingLibraryPtr getRenderingLibrary() { return m_RenderingLibrary; }

    //! Updates the contents associated with all contexts.
    void updateContexts();
    
    //! Creates a window with specified parameters.
    inline WindowPtr createWindow(const WindowDescription& desc) { return m_WindowManager->createWindow(m_RenderingLibrary, desc); }

	CE_DECLARE_ATTRIBUTE(ContextVector, CE_RANGE_NONE, Contexts, "Rendering contexts")
	CE_DECLARE_CONST_ATTRIBUTE(ResourceCachePtr, CE_RANGE_NONE, ResourceCache, "Resource cache")

	CE_ATTRIBUTES(CE_STATIC, NullBaseClass,
				  CE_CREATE_ATTRIBUTE(Contexts),
				  CE_CREATE_ATTRIBUTE(ResourceCache))
};

/*! \brief Represents a complete set of graphics-related objects that manager the creation and rendering of scene objects.
 * 
 *  The main point of having more than one of this objects is to have separate windows of different rendering system running
 *  together without launching another application instance. Most of the applications might use just a single one of these.
 */
class GraphicsEngineContext
{
    //! The window associated with this graphics context.
    WindowPtr           m_Window;
    
    //! The resource manager used for caching objects associated with this rendering context.
    ResourceManagerPtr  m_ResMgr;
    
    //! The lighting manager used for creating instances of predefined light types.
    LightingManagerPtr  m_LightingMgr;
    
    //! The world manager used for collision queries.
    WorldManagerPtr     m_WorldMgr;
    
    //! The node manager used for creating scene nodes.
    NodeManagerPtr      m_NodeMgr;
public:
    /*! \brief Constructor.
     *  \param cge              a reference to the graphics engine main instance.
     *  \param ctx_settings     the settings which are used for this context.
     */
    GraphicsEngineContext(GraphicsEngine& cge, ContextSettings& ctx_settings);

    //! Returns the resource manager used for caching data in a manner specific to the settings of this context.
    ResourceManagerPtr getResourceManager() const { return m_ResMgr; }
    
    //! Returns the lighting manager used for creating instances of predefined light types.
    LightingManagerPtr getLightingManager() const { return m_LightingMgr; }
    
    //! Returns the world manager used for querying for collisions between objects.
    WorldManagerPtr getWorldManagerManager() const { return m_WorldMgr; }
    
    //! Returns the node manager used for creating scene nodes.
    NodeManagerPtr getNodeManager() const { return m_NodeMgr; }
    
    //! Returns the window associated with this context.
    WindowPtr getWindow() { return m_Window; }

    //! Updates the contents associated with this context.
    void updateContext();
    
    //! Swaps the display frame buffers, so that the rendering of a new frame can begin.
    void swapBuffers() { m_Window->swapBuffers(); }
};
}

#endif /* GRAPHICS_ENGINE_HH_ */
